#mapping-script
# -*- coding: ms949 -*-
import bots.transform as transform
import bots.usersys.mappings.utils as utils

def main(inn,out):
    #XML
    ns1 = '{urn:kr:gov:kcs:data:standard:KCS_DeclarationOfReleaseOfContainerSchemaModule:1:0:0}'
    ns2 = '{urn:kr:gov:kcs:common:standard:KCS_ReusableAggregateBusinessInformationEntitySchemaModule:1:0:0}'
    rootelement = ns1 + 'DeclarationOfReleaseOfContainer'
        
    #Read from inn by EDIFACT order
    DocumentTypeCode = inn.get({'BOTSID':'UNH'},{'BOTSID':'BGM','C002.1001':None})
    DocumentReferenceID = inn.get({'BOTSID':'UNH'},{'BOTSID':'BGM','1004':None})
    
    ReleaseDateTime = inn.get({'BOTSID':'UNH'},{'BOTSID':'DTM','C507.2005':'261','C507.2380':None})
    
    CustomsOfficeCodeID = inn.get({'BOTSID':'UNH'},{'BOTSID':'LOC','C517.1131':'113','C517.3225':None})
    CustomsOfficeCode = inn.get({'BOTSID':'UNH'},{'BOTSID':'LOC','C519.1131':'5AB','C519.3223':None})
    
    ContainerNumberID = inn.get({'BOTSID':'UNH'},{'BOTSID':'RFF','C506.1153':'AAQ','C506.1154':None})
    
    
    out.ta_info['botskey'] = 'CUSBRR-5LB-' + DocumentReferenceID.strip()
    
    #Write to out by XML order
    
    #Loop 1 ~ 99
    for gid in inn.getloop({'BOTSID':'UNH'},{'BOTSID':'GID'}):
        ReleaseOfContainerSequenceID = gid.get({'BOTSID':'GID','C213#1.7224':None})
        ReleaseOfContainerReleaseTypeCode = gid.get({'BOTSID':'GID','C213#1.7065':None})
        ReleaseOfContainerReleasePeriodExtensionTypeCode = gid.get({'BOTSID':'GID'},{'BOTSID':'GIS','C529.1131':'5DM','C529.7365':None})
        
        UCRIdentificationID = gid.get({'BOTSID':'GID'},{'BOTSID':'RFF','C506.1153':'XC','C506.1154':None})
        UCRMasterBLSequenceID = gid.get({'BOTSID':'GID'},{'BOTSID':'RFF','C506.1153':'XC','C506.1156':None})
        UCRHouseBLSequenceID = gid.get({'BOTSID':'GID'},{'BOTSID':'RFF','C506.1153':'XC','C506.4000':None})
        
        PreviousCustomsDocumentReferenceID = gid.get({'BOTSID':'GID'},{'BOTSID':'RFF','C506.1153':'ABP','C506.1154':None})
        PeriodExtensionPreviousCustomsDocumentReferenceID = gid.get({'BOTSID':'GID'},{'BOTSID':'RFF','C506.1153':'ABL','C506.1154':None})
    
        lou = out.putloop({'BOTSID':rootelement,'BOTSID':ns1+'ReleaseOfContainer'})
        lou.put({'BOTSID':ns1+'ReleaseOfContainer',ns2+'SequenceID':ReleaseOfContainerSequenceID})
        lou.put({'BOTSID':ns1+'ReleaseOfContainer',ns2+'ReleaseTypeCode':ReleaseOfContainerReleaseTypeCode})
        lou.put({'BOTSID':ns1+'ReleaseOfContainer',ns2+'ReleasePeriodExtensionTypeCode':ReleaseOfContainerReleasePeriodExtensionTypeCode})
        
        lou.put({'BOTSID':ns1+'ReleaseOfContainer'},{'BOTSID':ns2+'UCR',ns2+'IdentificationID':UCRIdentificationID})
        lou.put({'BOTSID':ns1+'ReleaseOfContainer'},{'BOTSID':ns2+'UCR',ns2+'MasterBLSequenceID':UCRMasterBLSequenceID})
        lou.put({'BOTSID':ns1+'ReleaseOfContainer'},{'BOTSID':ns2+'UCR',ns2+'HouseBLSequenceID':UCRHouseBLSequenceID})
        
        lou.put({'BOTSID':ns1+'ReleaseOfContainer'},{'BOTSID':ns2+'PreviousCustomsDocument',ns2+'referenceID':PreviousCustomsDocumentReferenceID})
        lou.put({'BOTSID':ns1+'ReleaseOfContainer'},{'BOTSID':ns2+'PeriodExtensionPreviousCustomsDocument',ns2+'referenceID':PeriodExtensionPreviousCustomsDocumentReferenceID})
    #Loop End

    out.put({'BOTSID':rootelement,ns1+'DeclarationOfReleaseOfContainer__{http://www.w3.org/2001/XMLSchema-instance}schemaLocation':'urn:kr:gov:kcs:data:standard:KCS_DeclarationOfReleaseOfContainerSchemaModule:1:0:0 ./schema/uncefact/data/KCS_DeclarationOfReleaseOfContainerSchemaModule_1.0.0_standard.xsd'})
    
    #Write to out by XML order
    out.put({'BOTSID':rootelement,ns1+'ReleaseDateTime':utils.isoDateTimeFormat(ReleaseDateTime)})
    out.put({'BOTSID':rootelement,ns1+'ContainerNumberID':ContainerNumberID})
    
    out.put({'BOTSID':rootelement},{'BOTSID':ns1+'Document',ns2+'TypeCode':DocumentTypeCode})
    out.put({'BOTSID':rootelement},{'BOTSID':ns1+'Document',ns2+'referenceID':DocumentReferenceID})
    
    out.put({'BOTSID':rootelement},{'BOTSID':ns1+'CustomsOffice',ns2+'OfficeCode':CustomsOfficeCode})
    out.put({'BOTSID':rootelement},{'BOTSID':ns1+'CustomsOffice',ns2+'CodeID':CustomsOfficeCodeID})
    
